package com.share.module.business.stock.web;

import com.share.module.business.stock.entity.Stock;
import com.share.module.business.stock.service.StockService;
import com.share.module.business.stock.vo.*;
import com.stock.common.response.ApiResult;
import com.stock.mybatisplus.utils.PageUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 股票管理控制器
 * 提供股票的增删改查等基础操作
 *
 * @author system
 * @since 2025-11-02
 */
@Tag(name = "股票管理", description = "股票信息的增删改查接口")
@RequestMapping("/stock")
@RestController
public class StockController {

    @Resource
    private StockService stockService;

    @Operation(summary = "分页查询股票", description = "根据条件分页查询股票列表")
    @GetMapping("/page")
    public ApiResult<PageUtils<StockPageVO>> findPage(
            @Parameter(description = "分页查询条件") StockPageQueryVO vo) {
        PageUtils<StockPageVO> pageUtils = stockService.page(vo);
        return ApiResult.success(pageUtils);
    }

    @Operation(summary = "保存或更新股票", description = "新增或更新股票信息,有id则更新,无id则新增")
    @PostMapping
    public ApiResult<Boolean> save(
            @Valid @RequestBody @Parameter(description = "股票保存对象") StockSaveVO vo) {
        Stock stock = new Stock();
        stock.setId(vo.getId());
        stock.setName(vo.getName());
        stock.setCode(vo.getCode());
        stock.setUrl(vo.getUrl());
        if (stock.getId() != null) {
            stockService.updateById(stock);
        } else {
            stockService.save(stock);
        }
        return ApiResult.success();
    }

    @Operation(summary = "批量删除股票", description = "根据ID列表批量删除股票")
    @PostMapping("/del/batch")
    public ApiResult<Boolean> batchDel(
            @RequestBody @Parameter(description = "股票ID列表") List<Long> ids) {
        return ApiResult.success(stockService.removeByIds(ids));
    }

    @Operation(summary = "删除股票", description = "根据ID删除单个股票")
    @DeleteMapping("/{id}")
    public ApiResult<Boolean> delete(
            @PathVariable @Parameter(description = "股票ID") String id) {
        return ApiResult.success(stockService.removeById(id));
    }

    @Operation(summary = "查询所有股票", description = "查询所有股票列表(不分页)")
    @GetMapping
    public ApiResult<List<StockListVO>> findAll() {
        List<Stock> list = stockService.list();
        List<StockListVO> targets = list.stream().map(item -> {
            StockListVO listVO = new StockListVO();
            listVO.setId(item.getId());
            listVO.setName(item.getName());
            listVO.setUrl(item.getUrl());
            return listVO;
        }).collect(Collectors.toList());
        return ApiResult.success(targets);
    }

    @Operation(summary = "查询股票详情", description = "根据ID查询股票详细信息")
    @GetMapping("/{id}")
    public ApiResult<StockViewVO> findOne(
            @PathVariable @Parameter(description = "股票ID") Long id) {
        Stock stock = stockService.getById(id);
        StockViewVO viewVO = new StockViewVO();
        viewVO.setId(stock.getId());
        viewVO.setName(stock.getName());
        viewVO.setCode(stock.getCode());
        viewVO.setUrl(stock.getUrl());
        return ApiResult.success(viewVO);
    }
}
